package com.csoa.community.activity;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.message.BasicNameValuePair;

import android.content.Intent;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.csoa.community.R;
import com.csoa.community.bean.RiChengModel;
import com.csoa.community.net.WcfAsyncTask;
import com.csoa.community.requests.AbstractRequest;
import com.csoa.community.requests.RiChengRequest;
import com.csoa.community.singletons.GlobalVars;
import com.csoa.community.widget.PullToRefreshBase.OnRefreshListener;
import com.csoa.community.widget.NarView;
import com.csoa.community.widget.PullToRefreshListView;

public class RiChengActivity extends BaseActivity implements OnClickListener {

	PullToRefreshListView mPullList;
	ListView  mList;
	ProgressBar mProgress;
	EditText mEdit;

	MAdapter mAdapter;
	public static List<RiChengModel> data;
	private final String pageSize = "20";
	private static int pageNo = 1;

	int preCount = 0;
	protected boolean isReload = true;
	private NarView narView;
	
	@Override
	public void initUI() {
		setContentView(R.layout.richeng);
		initNarView();
		this.findViewById(R.id.tz_search_btn).setOnClickListener(this);

		mPullList = (PullToRefreshListView) this.findViewById(R.id.tz_list);
		mList = mPullList.getRefreshableView();
		mProgress = (ProgressBar) this.findViewById(R.id.tz_progress);
		mEdit = (EditText) this.findViewById(R.id.tz_search_edit);
		super.initUI();
	}
	
	private void initNarView() {
		narView = (NarView)this.findViewById(R.id.comNarView);
		narView.enterModule(R.id.nav_daily);
	}

	@Override
	public void initData() {
		
		if (data == null) {
			data = new ArrayList<RiChengModel>();
		}
		data.clear();
		
		mAdapter = new MAdapter();
		mList.setAdapter(mAdapter);
		mPullList.setOnRefreshListener(new OnRefreshListener() {
			@Override
			public void onRefresh() {
				if(mPullList.hasPullFromTop()) {
					pageNo = 1;
					isReload = true;
				}else {
					isReload = false;
				}
				buildRequestGetRiCheng(true);
			}
		});
		
		mList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				Intent intent = new Intent(RiChengActivity.this, RiChengXQActivity.class);
				intent.putExtra(RiChengXQActivity.extra_tongzhi, data.get(position));
				intent.putExtra(RiChengXQActivity.extra_position, position);
				startActivity(intent);
			}
		});
		
		pageNo = 1;
		buildRequestGetRiCheng(false);

		super.initData();
	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.tz_search_btn:
			break;
		default:
			break;
		}
	}

	protected void buildRequestGetRiCheng(boolean isAdd) {
		mPullList.showHeadView();
		initCenterUI(isAdd);
		List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("LogName", GlobalVars.currentUser.loginName));
		params.add(new BasicNameValuePair("PageSize", pageSize));
		params.add(new BasicNameValuePair("CurrPage", pageNo + ""));
		netRequestGetRiCheng(new RiChengRequest(params, this));
	}

	private void netRequestGetRiCheng(AbstractRequest request) {
		new WcfAsyncTask(this, new OnUICallback() {
			@Override
			public void onGetResult(AbstractRequest request) {
				initCenterUI(true);
				RiChengRequest mrq = (RiChengRequest) request;
				mPullList.onRefreshComplete();
				if (mrq.data != null) {
					if(isReload  ) {
						if (mrq.data.size() == 0) {
							data.clear();
							mAdapter.notifyDataSetChanged();
							RiChengActivity.this.showToast("暂无日程");
							return;
						}
						pageNo ++;
						data.clear();
					}else {
						if (mrq.data.size() == 0) {
							RiChengActivity.this.showToast("没有更多日程了");
							return;
						}
						pageNo ++;
					}
					data.addAll(mrq.data);
					mAdapter.notifyDataSetChanged();
				}
			}

			@Override
			public void onCancel() {
				initCenterUI(true);
				mPullList.onRefreshComplete();
			}

			@Override
			public void onProgress(int progress, String msg) {
				RiChengActivity.this.showToast(msg);
			}
		}).execute(request);
	}

	private void initCenterUI(boolean isShowList) {
		if (isShowList) {
			if (mList.getVisibility() != View.VISIBLE) {
				mList.setVisibility(View.VISIBLE);
			}
			if (mProgress.getVisibility() != View.INVISIBLE) {
				mProgress.setVisibility(View.INVISIBLE);
			}
		} else {
			if (mList.getVisibility() != View.INVISIBLE) {
				mList.setVisibility(View.INVISIBLE);
			}
			if (mProgress.getVisibility() != View.VISIBLE) {
				mProgress.setVisibility(View.VISIBLE);
			}
		}
	}

	private class MAdapter extends BaseAdapter {

		@Override
		public int getCount() {
			return data.size();
		}

		@Override
		public Object getItem(int position) {
			return position;
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			Holder1 holder;
			RiChengModel model = data.get(position);
			if (convertView == null
					|| !(convertView.getTag() instanceof Holder1)) {
				convertView = getLayoutInflater().inflate(
						R.layout.richeng_item, null);
				holder = new Holder1();
				holder.time = (TextView) convertView.findViewById(R.id.tzi_shijian);
				holder.name = (TextView) convertView.findViewById(R.id.tzi_tongzhi);
			} else {
				holder = (Holder1) convertView.getTag();
			}

			holder.name.setText(model.name);
			try {
				holder.time.setText((new SimpleDateFormat("yyyy-MM-dd HH:mm").format(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").parse(model.time_start))));
			} catch (ParseException e) {
				e.printStackTrace();
			}
			
			return convertView;
		}
	}

	private static final class Holder1 {
		TextView name;
		TextView time;
	}

}
